package service;

import mapper.HostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.Host;
import po.HostCustom;
import po.Result;

import java.util.List;

@Service
public class HostServiceImpl implements HostService {
    @Autowired
    private HostMapper hostMapper;

    @Override
    public List<HostCustom> findAllHostCustom() throws Exception {
        return hostMapper.findAllHostCustom();
    }

    @Override
    public Result addHost(Host host) {
        Result result = new Result();
        try {
            hostMapper.addHost(host);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("添加失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("添加成功！");
        return result;
    }

    @Override
    public Result deleteHostById(int id) {
        Result result = new Result();
        try {
            hostMapper.deleteHostById(id);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("删除失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("删除成功！");
        return result;
    }

    @Override
    public Result updateHostById(int id, Host host) {
        Result result = new Result();
        try {
            host.setId(id);
            hostMapper.updateHostById(host);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("修改失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("修改成功！");
        return result;
    }

    @Override
    public Host findHostById(int id) throws Exception {
        return hostMapper.findHostById(id);
    }
}
